/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Frame_3_ExamMainFrame.java]
 * Creation Date        [12-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_04_other.examApp.ui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Timer;

import pkg_04_other.examApp.dao.ExaminationFactory;
import pkg_04_other.examApp.entity.Paper;
import pkg_04_other.examApp.entity.Question;

/**
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Frame_3_ExamMainFrame extends JFrame implements ActionListener {

	private static final long serialVersionUID = -7923584274964900453L;

	/** 主窗口相关组件 */
	private final JButton bt_previous = new JButton("<<上一题");
	private final JButton bt_next = new JButton("下一题>>");
	private final JButton bt_submit = new JButton("交卷");
	private final JTextArea jta_paper = new JTextArea(20, 40);
	private Frame_4_QuestionPanel options = null;
	private final JLabel label1 = new JLabel("倒计时");
	private final JLabel label2 = new JLabel();
	private int totalTime = 3600;
	private final Timer timer = new Timer(1000, this);

	/** 题目总数 */
	private final int questionCount = ExaminationFactory
			.getExaminationInstance("paper").getCount();
	/** 题目编号 */
	private int questionId = 1;
	/** 剩余题数 */
	private final JLabel label3 = new JLabel("剩余题数");
	private final JLabel label4 = new JLabel((questionCount - questionId) + "");
	/** 科目名称 */
	private final String subjectName = ExaminationFactory
			.getExaminationInstance("paper").getSubject();
	/** 科目试卷 */
	private final Paper paper = ExaminationFactory.getExaminationInstance(
			"paper").getPaper();
	String text = "";

	private void init() {
		// 北部面板
		JPanel jpNorth = new JPanel();
		JLabel jLabel01 = new JLabel("欢迎进入考试系统");
		String id = ExaminationFactory.getExaminationInstance("student")
				.getId() + "";
		JLabel jLabel02 = new JLabel("考生编号:" + id);
		String name = ExaminationFactory.getExaminationInstance("student")
				.getName();
		JLabel jLabel03 = new JLabel("考生姓名:" + name);
		JLabel jLabel04 = new JLabel("考试科目:" + subjectName);
		jpNorth.add(jLabel01);
		jpNorth.add(jLabel02);
		jpNorth.add(jLabel03);
		jpNorth.add(jLabel04);
		this.add(jpNorth, "North");

		// 中部面板
		jta_paper.setEditable(false);
		jta_paper.setText(getQuestionText());
		this.add(jta_paper, "Center");

		// 南部面板
		JPanel jpSouth = new JPanel();
		options = new Frame_4_QuestionPanel();
		JLabel jLabel05 = new JLabel("请选择:  ");
		jpSouth.add(jLabel05);
		jpSouth.add(options);
		jpSouth.add(bt_previous);
		jpSouth.add(bt_next);
		jpSouth.add(bt_submit);
		bt_previous.addActionListener(this);
		bt_next.addActionListener(this);
		bt_submit.addActionListener(this);
		this.add(jpSouth, "South");

		// 西部面板
		JPanel jpWest = new JPanel(new GridLayout(2, 1));
		jpWest.add(label3);
		jpWest.add(label4);
		JPanel pw = new JPanel();
		pw.add(jpWest);
		this.add(pw, "West");

		// 东部面板
		JPanel jpEast = new JPanel(new GridLayout(2, 1));
		jpEast.add(label1);
		jpEast.add(label2);
		JPanel pe = new JPanel();
		pe.add(jpEast);
		this.add(pe, "East");

		// 启动定时器
		timer.start();
	}

	public Frame_3_ExamMainFrame() {
		init();
		String title = "-=考试系统=- " + subjectName;
		setTitle(title);
		this.setLocation(200, 200);
		setVisible(true);
		setResizable(false);
		pack();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	/**
	 * 获得试题
	 * 
	 * @return
	 */
	private String getQuestionText() {
		// 通过ID获得试题信息
		Question question = paper.getQuestions().get(questionId);
		String[] info = new String[4];
		try {
			for (int i = 0; i < info.length; i++) {
				info[i] = question.getOptions().get(i);
				if (info[i].startsWith("<T>")) {
					info[i] = info[i].substring(3);
				}
			}
			text = questionId + "." + question.getQuestion() + "\nA." + info[0]
					+ "\nB." + info[1] + "\nC." + info[2] + "\nD." + info[3];
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "没有试卷生成");
		}
		return text;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();

		if ("<<上一题".equals(command)) {
			System.out.println("<<上一题");
			// 将选项放入题目回答的集合中
			ExaminationFactory.getExaminationInstance("score").getAnswers()
					.put(questionId, options.getSelectedValue());
			// 答题完毕后清空选项
			options.getGroup().clearSelection();
			if (questionId > 1) {
				questionId--;
				jta_paper.setText(getQuestionText());
				label4.setText((questionCount - questionId) + "");
			}
		}

		if ("下一题>>".equals(command)) {
			System.out.println("下一题>>");
			// 将选项放入题目回答的集合中
			ExaminationFactory.getExaminationInstance("score").getAnswers()
					.put(questionId, options.getSelectedValue());
			options.getGroup().clearSelection();
			if (questionId >= questionCount) {
				JOptionPane.showMessageDialog(this, "试题已全部答完, 请您检查一下准备交卷");
			} else {
				questionId++;
				jta_paper.setText(getQuestionText());
				label4.setText((questionCount - questionId) + "");
			}
		}

		if ("交卷".equals(command)) {
			ExaminationFactory.getExaminationInstance("score").getAnswers()
					.put(questionId, options.getSelectedValue());
			options.getGroup().clearSelection();
			float score = ExaminationFactory.getExaminationInstance("score")
					.getScore();
			JOptionPane.showMessageDialog(this, "您本次的成绩为" + score);
			System.exit(0);
		}

		// 倒计时
		totalTime--;
		if (totalTime <= 0) {
			label2.setText("00:00:00");
			JOptionPane.showMessageDialog(this, "时间已到, 系统自动交卷");
			float score = ExaminationFactory.getExaminationInstance("score")
					.getScore();
			JOptionPane.showMessageDialog(this, "您本次的成绩为" + score);
			System.exit(0);
		}
		int hour = totalTime / 3600;
		int minute = (totalTime % 3600) / 60;
		int second = (totalTime % 3600) % 60;

		String ph = hour < 10 ? "0" : "";
		String pm = minute < 10 ? "0" : "";
		String ps = second < 10 ? "0" : "";
		String time = ph + hour + ":" + pm + minute + ":" + ps + second;
		label2.setText(time);
	}
}
